package rtp.servlets;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import rtp.model.Tarefa;
import rtp.model.dao.ITarefaDAO;
import rtp.model.dao.TarefaDAO;
import rtp.model.manager.TarefaManager;

/**
 * Servlet implementation class ConcluirTarefasServlet
 */
@WebServlet("/ConcluirTarefasServlet")
public class ConcluirTarefasServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ConcluirTarefasServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String SSerials = request.getParameter("ids");
		String Serials[] = SSerials .split("\\,");
		
		String path = getServletContext().getInitParameter("Dados");
		ITarefaDAO dao = new TarefaDAO(path);
		TarefaManager manager = new TarefaManager(dao);		
		
		for (int i = 0; i < Serials.length; i++) {
			String buscaId = Serials[i];
			Tarefa tTarefa = manager.retornarTarefa(buscaId);
			tTarefa.setDtConclusao(request.getParameter("txtData"));
			tTarefa.setStatus("F");
			tTarefa.setSolucao(request.getParameter("txtSolucao"));
			manager.concluirTarefa(tTarefa);
		}
		
		request.setAttribute("tarefas", "F");
		RequestDispatcher view = request.getRequestDispatcher("listarTarefas.jsp");
		
		view.forward(request, response);
	}

}
